| Conditions | 1 |
| Paths | 108 |
| Total Lines | 160 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 29 | })(function (loadImage) { |
||
| 30 | 'use strict' |
||
| 31 | |||
| 32 | var originalHasCanvasOption = loadImage.hasCanvasOption |
||
| 33 | var originalHasMetaOption = loadImage.hasMetaOption |
||
| 34 | var originalTransformCoordinates = loadImage.transformCoordinates |
||
| 35 | var originalGetTransformedOptions = loadImage.getTransformedOptions |
||
| 36 | |||
| 37 | // Determines if the target image should be a canvas element: |
||
| 38 | loadImage.hasCanvasOption = function (options) { |
||
| 39 | return ( |
||
| 40 | !!options.orientation || originalHasCanvasOption.call(loadImage, options) |
||
| 41 | ) |
||
| 42 | } |
||
| 43 | |||
| 44 | // Determines if meta data should be loaded automatically: |
||
| 45 | loadImage.hasMetaOption = function (options) { |
||
| 46 | return ( |
||
| 47 | (options && options.orientation === true) || |
||
| 48 | originalHasMetaOption.call(loadImage, options) |
||
| 49 | ) |
||
| 50 | } |
||
| 51 | |||
| 52 | // Transform image orientation based on |
||
| 53 | // the given EXIF orientation option: |
||
| 54 | loadImage.transformCoordinates = function (canvas, options) { |
||
| 55 | originalTransformCoordinates.call(loadImage, canvas, options) |
||
| 56 | var ctx = canvas.getContext('2d') |
||
| 57 | var width = canvas.width |
||
| 58 | var height = canvas.height |
||
| 59 | var styleWidth = canvas.style.width |
||
| 60 | var styleHeight = canvas.style.height |
||
| 61 | var orientation = options.orientation |
||
| 62 | if (!orientation || orientation > 8) { |
||
| 63 | return |
||
| 64 | } |
||
| 65 | if (orientation > 4) { |
||
| 66 | canvas.width = height |
||
| 67 | canvas.height = width |
||
| 68 | canvas.style.width = styleHeight |
||
| 69 | canvas.style.height = styleWidth |
||
| 70 | } |
||
| 71 | switch (orientation) { |
||
|
|
|||
| 72 | case 2: |
||
| 73 | // horizontal flip |
||
| 74 | ctx.translate(width, 0) |
||
| 75 | ctx.scale(-1, 1) |
||
| 76 | break |
||
| 77 | case 3: |
||
| 78 | // 180° rotate left |
||
| 79 | ctx.translate(width, height) |
||
| 80 | ctx.rotate(Math.PI) |
||
| 81 | break |
||
| 82 | case 4: |
||
| 83 | // vertical flip |
||
| 84 | ctx.translate(0, height) |
||
| 85 | ctx.scale(1, -1) |
||
| 86 | break |
||
| 87 | case 5: |
||
| 88 | // vertical flip + 90 rotate right |
||
| 89 | ctx.rotate(0.5 * Math.PI) |
||
| 90 | ctx.scale(1, -1) |
||
| 91 | break |
||
| 92 | case 6: |
||
| 93 | // 90° rotate right |
||
| 94 | ctx.rotate(0.5 * Math.PI) |
||
| 95 | ctx.translate(0, -height) |
||
| 96 | break |
||
| 97 | case 7: |
||
| 98 | // horizontal flip + 90 rotate right |
||
| 99 | ctx.rotate(0.5 * Math.PI) |
||
| 100 | ctx.translate(width, -height) |
||
| 101 | ctx.scale(-1, 1) |
||
| 102 | break |
||
| 103 | case 8: |
||
| 104 | // 90° rotate left |
||
| 105 | ctx.rotate(-0.5 * Math.PI) |
||
| 106 | ctx.translate(-width, 0) |
||
| 107 | break |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | // Transforms coordinate and dimension options |
||
| 112 | // based on the given orientation option: |
||
| 113 | loadImage.getTransformedOptions = function (img, opts, data) { |
||
| 114 | var options = originalGetTransformedOptions.call(loadImage, img, opts) |
||
| 115 | var orientation = options.orientation |
||
| 116 | var newOptions |
||
| 117 | var i |
||
| 118 | if (orientation === true && data && data.exif) { |
||
| 119 | orientation = data.exif.get('Orientation') |
||
| 120 | } |
||
| 121 | if (!orientation || orientation > 8 || orientation === 1) { |
||
| 122 | return options |
||
| 123 | } |
||
| 124 | newOptions = {} |
||
| 125 | for (i in options) { |
||
| 126 | if (options.hasOwnProperty(i)) { |
||
| 127 | newOptions[i] = options[i] |
||
| 128 | } |
||
| 129 | } |
||
| 130 | newOptions.orientation = orientation |
||
| 131 | switch (orientation) { |
||
| 132 | case 2: |
||
| 133 | // horizontal flip |
||
| 134 | newOptions.left = options.right |
||
| 135 | newOptions.right = options.left |
||
| 136 | break |
||
| 137 | case 3: |
||
| 138 | // 180° rotate left |
||
| 139 | newOptions.left = options.right |
||
| 140 | newOptions.top = options.bottom |
||
| 141 | newOptions.right = options.left |
||
| 142 | newOptions.bottom = options.top |
||
| 143 | break |
||
| 144 | case 4: |
||
| 145 | // vertical flip |
||
| 146 | newOptions.top = options.bottom |
||
| 147 | newOptions.bottom = options.top |
||
| 148 | break |
||
| 149 | case 5: |
||
| 150 | // vertical flip + 90 rotate right |
||
| 151 | newOptions.left = options.top |
||
| 152 | newOptions.top = options.left |
||
| 153 | newOptions.right = options.bottom |
||
| 154 | newOptions.bottom = options.right |
||
| 155 | break |
||
| 156 | case 6: |
||
| 157 | // 90° rotate right |
||
| 158 | newOptions.left = options.top |
||
| 159 | newOptions.top = options.right |
||
| 160 | newOptions.right = options.bottom |
||
| 161 | newOptions.bottom = options.left |
||
| 162 | break |
||
| 163 | case 7: |
||
| 164 | // horizontal flip + 90 rotate right |
||
| 165 | newOptions.left = options.bottom |
||
| 166 | newOptions.top = options.right |
||
| 167 | newOptions.right = options.top |
||
| 168 | newOptions.bottom = options.left |
||
| 169 | break |
||
| 170 | case 8: |
||
| 171 | // 90° rotate left |
||
| 172 | newOptions.left = options.bottom |
||
| 173 | newOptions.top = options.left |
||
| 174 | newOptions.right = options.top |
||
| 175 | newOptions.bottom = options.right |
||
| 176 | break |
||
| 177 | } |
||
| 178 | if (newOptions.orientation > 4) { |
||
| 179 | newOptions.maxWidth = options.maxHeight |
||
| 180 | newOptions.maxHeight = options.maxWidth |
||
| 181 | newOptions.minWidth = options.minHeight |
||
| 182 | newOptions.minHeight = options.minWidth |
||
| 183 | newOptions.sourceWidth = options.sourceHeight |
||
| 184 | newOptions.sourceHeight = options.sourceWidth |
||
| 185 | } |
||
| 186 | return newOptions |
||
| 187 | } |
||
| 188 | }) |
||
| 189 |